package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.widget.RoundImageView;

public class PersonalInfoFragment extends BaseFragment {
  public static final String EXTRA_PATH = "com.isunland.managesystem.ui.EXTRA_PATH";
  protected static final int REQUEST_IMAGE = 0;// 上传头像
  private RoundImageView ivUserPhoto;// 登录人头像
  private CurrentUser mUser;
  //private Bitmap mbitmap;
  private String mFilePath = "";

  public static PersonalInfoFragment newInstance(String path) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_PATH, path);
    PersonalInfoFragment fragment = new PersonalInfoFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    setTitleCustom(R.string.personal);
    mUser = CurrentUser.newInstance(getActivity());
    mFilePath = (String) getArguments().getSerializable(EXTRA_PATH);
    LogUtil.i("mPath=" + mFilePath);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragmet_personal_info, container, false);
    ivUserPhoto = (RoundImageView) v.findViewById(R.id.iv_userPhoto);
    TextView tvUserName = (TextView) v.findViewById(R.id.tv_userName);
    TextView tvMobile = (TextView) v.findViewById(R.id.tv_mobile);
    TextView tvMemberName = (TextView) v.findViewById(R.id.tv_memberName);
    TextView tvKeyPositionName = (TextView) v.findViewById(R.id.tv_keypositionName);
    TextView tvEmail = (TextView) v.findViewById(R.id.tv_email);
    TextView tvLoginname = (TextView) v.findViewById(R.id.tv_loginname);
    TextView tvDept = (TextView) v.findViewById(R.id.tv_dept);
    // 登录人头像显示
    //		mDocPath=FileUtil.getFilePathByThumbNailPath(mDocPath);
    if (!TextUtils.isEmpty(mFilePath)) {

      setImageBypath(mFilePath);
    }

    // 获得头像图片

    // ivUserPhoto.setDrawingCacheEnabled(true);
    // mbitmap=Bitmap.createBitmap(ivUserPhoto.getDrawingCache());
    // ivUserPhoto.setDrawingCacheEnabled(false);//冲区清空画图缓，否则，下一次用getDrawingCache()方法回去图像时，还是原来的图像
    ivUserPhoto.setOnClickListener(new OnClickListener() {

      @Override public void onClick(View arg0) {
        Intent i = new Intent(getActivity(), UserPhotoActivity.class);

        i.putExtra(UserPhotoFragment.EXTRA_PATH, mFilePath);// 传头像地址

        startActivityForResult(i, REQUEST_IMAGE);
      }
    });
    tvUserName.setText(mUser.getRealName());
    tvMobile.setText(mUser.getMobile());
    tvMemberName.setText(mUser.getMemberName());
    tvKeyPositionName.setText(mUser.getKeypositionName());
    tvEmail.setText(mUser.getEmail());
    tvLoginname.setText(mUser.getName());
    tvDept.setText(mUser.getDeptName());
    return v;
  }

  // 登录人头像显示
  public void setImageBypath(String filePath) {
    String urlImage = ApiConst.getDownLoadUrlByGet(filePath);
    ImageLoader imageLoader = RequestManager.getImageLoader();

    ImageListener listener =
        ImageLoader.getImageListener(ivUserPhoto, R.drawable.photo, R.drawable.photo);

    imageLoader.get(urlImage, listener);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    switch (requestCode) {
      case REQUEST_IMAGE:
        mFilePath = mUser.getPicture();// 头像路径
        //			if (!TextUtils.isEmpty(mDocPath)) {
        //				mDocPath=FileUtil.getThumbNailFilePath(mDocPath);
        //			LogUtil.i("headImage="+mUser.getPicture());
        //			}
        String path = data.getStringExtra("result");
        ivUserPhoto.setImageBitmap(BitmapFactory.decodeFile(path, null));
        //			setImageBypath(path);
        Intent i = new Intent();
        i.putExtra("result", path);
        getActivity().setResult(Activity.RESULT_OK, i);

        break;

      default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
